package com.customview.CustomLine;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.customview.R;

/**
 * Created by Administrator on 2017/7/13.
 */

public class CustomLine extends View {

    float mHStrok;
    //进度条长度
    public float Wspace;
    public float Space;
    int mColor = Color.parseColor("#1E88E5");
    Paint mPaint;
    //每次向前移动的大小
    float mDelta = 10;
    //开始x坐标
    float startX;


    public CustomLine(Context context) {
        this(context, null);
    }

    public CustomLine(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLine, defStyleAttr, 0);
        int Count = typedArray.getIndexCount();

        /**
         * Count 获取的数量  是在布局 设置的 自定义属性 个数
         * 如果没有设置自定义属性   对应的case 是不会走的  影响初始化赋值
         *
         */
        Log.d("初始化", "CustomLine: " + Count);
//        for (int i = 0; i < Count; i++) {
//
//            switch (typedArray.getIndex(i))
//            {
//                //属性
//                case R.styleable.CustomLine_mColor:
//                   mColor=typedArray.getColor(R.styleable.CustomLine_mColor,Color.RED);
//                    Log.d("初始化", "CustomLine: mColor："+mColor);
//                    break;
//                case R.styleable.CustomLine_mWspace:
//                    Wspace =typedArray.getDimensionPixelSize(R.styleable.CustomLine_mWspace, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,getResources().getDisplayMetrics()));
//                    Log.d("初始化", "CustomLine: Wspace："+Wspace);
//
//                    break;
//                case R.styleable.CustomLine_mDelta:
//                   mDelta=typedArray.getDimensionPixelSize(R.styleable.CustomLine_mDelta, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,15,getResources().getDisplayMetrics()));
//                    Log.d("初始化", "CustomLine: mDelta："+mDelta);
//
//                    break;
//                case R.styleable.CustomLine_mSpace:
//                    Space=typedArray.getDimension(R.styleable.CustomLine_mSpace,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,getResources().getDisplayMetrics()));
//                    Log.d("初始化", "CustomLine: Space："+Space);
//
//                    break;
//                case R.styleable.CustomLine_mHStrok:
//                    mHStrok=typedArray.getDimension(R.styleable.CustomLine_mHStrok,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,15,getResources().getDisplayMetrics()));
//                    Log.d("初始化", "CustomLine: mHStrok："+mHStrok);
//
//                    break;
//            }
//      }

        //获取对应的自定义属性
        mColor = typedArray.getColor(R.styleable.CustomLine_mColor, Color.RED);
        Log.d("初始化", "CustomLine: mColor：" + mColor);
        Wspace = typedArray.getDimensionPixelSize(R.styleable.CustomLine_mWspace, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
        Log.d("初始化", "CustomLine: Wspace：" + Wspace);
        mDelta = typedArray.getDimensionPixelSize(R.styleable.CustomLine_mDelta, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
        Log.d("初始化", "CustomLine: mDelta：" + mDelta);
        Space = typedArray.getDimension(R.styleable.CustomLine_mSpace, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()));
        Log.d("初始化", "CustomLine: Space：" + Space);
        mHStrok = typedArray.getDimension(R.styleable.CustomLine_mHStrok, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
        Log.d("初始化", "CustomLine: mHStrok：" + mHStrok);

        //回收
        typedArray.recycle();
        //画笔
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        //线条颜色 宽度
        mPaint.setStrokeWidth(mHStrok);
        // mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //get screen width
        float sw = this.getMeasuredWidth();
        if (startX >= sw + (Wspace + Space) - (sw % (Wspace + Space)))
        {
            startX = 0;
        }
        else
        {
            startX += mDelta;
        }
        // 以 staxtX 为中心   向左右两边 绘制

        float start = startX;
       //绘制 startX 右边的
        while (start < sw)
        {
            canvas.drawLine(start, 5, start + Wspace, 5, mPaint);
            start += (Wspace + Space);
        }

        start = startX - Space - Wspace;

        //绘制 startX 左边的
        while (start >= -Wspace)
        {
            canvas.drawLine(start, 5, start + Wspace, 5, mPaint);
            start -= (Wspace + Space);
        }
        Log.d("测试", "onDraw: " + "绘制");

        int index = 0;


        //延迟作用
        while (index < 70000) {
            index++;
            Log.d("测试", "onDraw: 延迟");
        }
        invalidate();

    }
}
